package com.ruoyi.wx.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.wx.entity.PtExp;
import com.ruoyi.wx.entity.PtExpClass;
import com.ruoyi.wx.entity.dto.OpenAndNoExpDTO;
import com.ruoyi.wx.entity.dto.PtExpDTO;
import com.ruoyi.wx.mapper.PtExpClassMapper;
import com.ruoyi.wx.service.IPtExpClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 班级实验对应表 服务实现类
 * </p>
 *
 * @author moxiu
 * @since 2022-03-04
 */
@Service
public class PtExpClassServiceImpl extends ServiceImpl<PtExpClassMapper, PtExpClass> implements IPtExpClassService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean openExp(Long classId, Long expId, Long date) {
        PtExpClass ptExpClass = getOne(Wrappers.lambdaQuery(PtExpClass.class).eq(PtExpClass::getClassId,classId).eq(PtExpClass::getExpId,expId).select(PtExpClass::getId));
        if (ptExpClass == null) {
            ptExpClass = new PtExpClass();
        }
        ptExpClass.setClassId(classId);
        ptExpClass.setEndTime(new Date(date));
        ptExpClass.setExpId(expId);
        return saveOrUpdate(ptExpClass);
    }

    @Override
    public OpenAndNoExpDTO getExpIsOpenByDeptId(Long classId, long[] allClassId) {
        List<PtExpDTO> ptExp = baseMapper.getOpenExp(classId);
        OpenAndNoExpDTO res = new OpenAndNoExpDTO();
        long[] openClassId = null;
        if (ptExp == null || ptExp.size() == 0) {
            res.setOpenExp(null);
        } else {
            res.setOpenExp(ptExp);
            openClassId = new long[ptExp.size()];
            for (int i = 0; i < ptExp.size(); i++) {
                openClassId[i] = ptExp.get(i).getId();
            }
        }
        List<PtExpDTO> noOpenExp = baseMapper.getNoOpenExp(openClassId, allClassId);
        res.setNoOpenExp(noOpenExp);
        return res;
    }
}
